package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import Modelo.Login;

import jdbc.ConnectionFactory;

public class LoginDAO {
	private Connection cnn;
	private PreparedStatement stmt;
	private ResultSet rs;
	private String sql;

	public boolean addLogin(Login login) throws SQLException {
		this.sql = "INSERT INTO slv_web.tblogin (login, senha) VALUES (?,?)";
		cnn = new ConnectionFactory().getConnection();

		try {
			stmt = cnn.prepareStatement(sql);
			
			stmt.setString(1, login.getLogin());
			stmt.setString(2, login.getSenha());

			stmt.execute();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			try {
				cnn.rollback();
			} catch (SQLException e1) {
				System.out.print(e1.getStackTrace());
			}
			return false;
		} finally {
			stmt.close();
			cnn.close();
		}
	}

	public boolean validaLogin(Login l) throws SQLException {
		this.sql = "SELECT * FROM slv_web.tblogin WHERE login=? AND senha=?";
		cnn = new ConnectionFactory().getConnection();
		
		try {
			stmt = cnn.prepareStatement(sql);
			stmt.setString(1, l.getLogin());
			stmt.setString(2, l.getSenha());
			rs = stmt.executeQuery();
			if (rs.next())
				return true;
			else
				return false;
		} catch (Exception e) {
			e.printStackTrace();
			try {
				cnn.rollback();
			} catch (SQLException e1) {
				System.out.print(e1.getStackTrace());
			}
			return false;
		} finally {
			stmt.close();
			cnn.close();
		} 
	}
}
